<?php

namespace App\Jobs\Vip;

use App\Constants\VipOrderStatusConstant;
use App\Models\MemberVipOrder;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class CancelOrder implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        MemberVipOrder::where('status', VipOrderStatusConstant::WAIT_PAY)
            ->where(function ($q) {
                return $q->where('created_at', '<', date('Y-m-d H:i:s', time() - 60 * 15));
            })->chunkById(500, function ($orders){
                MemberVipOrder::whereIn('id', $orders->pluck('id')->all())
                    ->update(['status' => VipOrderStatusConstant::CANCEL]);
            });
    }
}
